package com.lmpop.trade.ti_advpara;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ti_advparaInfo implements TradeInterf
{
  QueryList queryList;

  public Ti_advparaInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_advpara ti_advpara = new Ti_advpara();

    ti_advpara.setPos_id(map.getString("pos_id"));
    ti_advpara.setCust_id(map.getString("cust_id"));
    ti_advpara.setAdv_type(map.getString("adv_type"));
    ti_advpara.setPos_name(map.getString("pos_name"));
    ti_advpara.setContent(map.getString("content"));
    ti_advpara.setPos_num(map.getInt("pos_num"));
    ti_advpara.setWord_num(map.getInt("word_num"));
    ti_advpara.setPrice(map.getString("price"));
    ti_advpara.setPage_url(map.getString("page_url"));
    ti_advpara.setNo_show(map.getString("no_show"));
    ti_advpara.setState_code(map.getString("state_code"));
    ti_advpara.setUser_id(map.getString("user_id"));
    ti_advpara.setIn_date(map.getString("in_date"));
    ti_advpara.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_advpara);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_advpara ti_advpara)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_advpara", ti_advpara);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_advpara", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_advpara ti_advpara = new Ti_advpara();
    ti_advpara.setPos_id(map.getString("pos_id"));
    ti_advpara.setCust_id(map.getString("cust_id"));
    ti_advpara.setAdv_type(map.getString("adv_type"));
    ti_advpara.setPos_name(map.getString("pos_name"));
    ti_advpara.setContent(map.getString("content"));
    ti_advpara.setPos_num(map.getInt("pos_num"));
    ti_advpara.setWord_num(map.getInt("word_num"));
    ti_advpara.setPrice(map.getString("price"));
    ti_advpara.setPage_url(map.getString("page_url"));
    ti_advpara.setNo_show(map.getString("no_show"));
    ti_advpara.setState_code(map.getString("state_code"));
    ti_advpara.setUser_id(map.getString("user_id"));
    ti_advpara.setIn_date(map.getString("in_date"));
    ti_advpara.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_advpara);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_advpara ti_advpara)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_advpara", ti_advpara);
  }

  public SysMap updateSort(DbmInterf dbmMgr, SysMap map)
  {
    Ti_advpara ti_advpara = new Ti_advpara();

    String _pos_id = map.getString("pos_id");
    String state_code = map.getString("state_code");

    String result_code = "0";
    try
    {
      if (!_pos_id.equals("")) {
        String[] _pos = _pos_id.split("\\|");
        for (int i = 0; i < _pos.length; i++)
          if (!_pos[i].equals("")) {
            ti_advpara.setPos_id(_pos[i]);
            ti_advpara.setState_code(state_code);
            updateSort(dbmMgr, ti_advpara);
          }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSort(DbmInterf dbmMgr, Ti_advpara ti_advpara)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_advparaSet", ti_advpara);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_advpara ti_advpara = (Ti_advpara)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_advpara.setStart(Integer.valueOf(start));
    ti_advpara.setLimit(Integer.valueOf(limit));

    return this.queryList.getList("getTi_advparaByList", ti_advpara);
  }

  public int getCountByObj(Object obj)
  {
    Ti_advpara ti_advpara = (Ti_advpara)obj;

    List list = this.queryList.getList("getTi_advparaListCount", ti_advpara);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_advparaByPk", pk);
  }

  public String getPosNameByPk(String pk) {
    List list = this.queryList.getList("getTi_advparaByPk", pk);
    String shortName = "";
    if ((list != null) && (list.size() > 0)) {
      Map m = (Hashtable)list.get(0);
      if ((m != null) && (m.get("pos_name") != null)) {
        shortName = m.get("pos_name").toString();
      }
    }
    return shortName;
  }

  public Map getAll(Object obj)
  {
    Ti_advpara ti_advpara = (Ti_advpara)obj;
    Map params = new Hashtable();
    String cust_id = ti_advpara.getCust_id();
    if ((cust_id != null) && (!cust_id.equals(""))) params.put("cust_id", cust_id);

    List advList = this.queryList.getList("getTi_advparaSearch", params);

    Map allAdvMap = new Hashtable();
    String advSelect = "";
    String para_pos_id = "";
    if (advList != null) {
      for (int i = 0; i < advList.size(); i++) {
        Hashtable advMap = (Hashtable)advList.get(i);
        String pos_id = ""; String pos_name = "";
        if (advMap.get("pos_id") != null) {
          pos_id = advMap.get("pos_id").toString();
        }
        if (advMap.get("pos_name") != null) {
          pos_name = advMap.get("pos_name").toString();
        }
        allAdvMap.put(pos_id, pos_name);
      }
    }
    return allAdvMap;
  }

  public String getAdvparaSelected(Object obj)
  {
    Ti_advpara ti_advpara = (Ti_advpara)obj;
    Map allAdvMap = new Hashtable();
    Map params = new Hashtable();
    String cust_id = ti_advpara.getCust_id();
    String para_pos_id = "";

    if (ti_advpara.getPos_id() != null) para_pos_id = ti_advpara.getPos_id();

    if ((cust_id != null) && (!cust_id.equals(""))) params.put("cust_id", cust_id);

    params.put("state_code", "0");

    String advSelect = ""; String pos_id = ""; String pos_name = "";

    List advList = this.queryList.getList("getTi_advparaSearch", params);

    if (advList != null) {
      for (int i = 0; i < advList.size(); i++) {
        Hashtable advMap = (Hashtable)advList.get(i);

        if (advMap.get("pos_id") != null) {
          pos_id = advMap.get("pos_id").toString();
        }
        if (advMap.get("pos_name") != null) {
          pos_name = advMap.get("pos_name").toString();
        }

        if (para_pos_id.equals(pos_id))
          advSelect = advSelect + "<option value=" + pos_id + " selected >" + pos_name + "</option>";
        else {
          advSelect = advSelect + "<option value=" + pos_id + ">" + pos_name + "</option>";
        }
      }
    }
    return advSelect;
  }
}